package com.tianyang.yukuaifu.common.utils;

import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tianyang.yukuaifu.utils.IntentTool;
import com.tianyang.yukuaifu.utils.ScreenUtil;
import com.tianyang.yukuaifu.utils.ToastUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static android.content.Context.CLIPBOARD_SERVICE;

/**
 * Description：公共工具类
 * Created by 薛勇军 on 2018/6/27.
 */

public class CommonUtils {

    public static String getMerchantName(int mchType) {
        switch (mchType) {
            case 0:
                return "企业";
            case 1:
                return "小微";
            case 2:
                return "个体";
        }
        return "小微";
    }

    /**
     * 把隐藏掉完成银行卡号
     *
     * @return
     */
    public static String secretBankNumber(String bankNo) {
        if (TextUtils.isEmpty(bankNo)) return "";
        try {
            CharSequence secret = bankNo.subSequence(bankNo.length() - 4, bankNo.length());
            return "************" + secret;
        } catch (Exception e) {
            e.printStackTrace();
            return bankNo;
        }
    }

    /**
     * 名字隐藏替换
     *
     * @return
     */
    public static String secretIdName(String name) {
        if (TextUtils.isEmpty(name)) return "";
        try {
            CharSequence secret = name.subSequence(0, name.length() - 1);
            return name.replace(secret, secret.length() == 1 ? "*" : "**");
        } catch (Exception e) {
            e.printStackTrace();
            return name;
        }
    }

    /**
     * 身份证隐藏替换
     */
    public static String secretIdNumber(String number) {
        if (TextUtils.isEmpty(number)) return "";
        try {
            CharSequence start = number.subSequence(0, 4);
            CharSequence end = number.subSequence(number.length() - 4, number.length());

            return start + "**********" + end;
        } catch (Exception e) {
            e.printStackTrace();
            return number;
        }
    }

    /**
     * 手机号码隐藏替换
     */
    public static String secretPhoneNumber(String number) {
        if (TextUtils.isEmpty(number)) return "";
        try {
            CharSequence start = number.subSequence(0, 3);
            CharSequence end = number.subSequence(number.length() - 4, number.length());

            return start + "****" + end;
        } catch (Exception e) {
            e.printStackTrace();
            return number;
        }
    }

    /**
     * 验证是否是数字
     */
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 分段格式化银行卡号码(xxxx-xxxx-xxxx-xxxx-xxx格式)
     */
    public static String bankNumFormatter(String number) {
        String ret;
        int len = number.length();

        if (len <= 4) {
            ret = number;
        } else if (len <= 8) {
            String s1 = number.substring(0, 4);
            String s2 = number.substring(4, len);
            ret = s1 + " " + s2;
        } else if (len <= 12) {
            String s1 = number.substring(0, 4);
            String s2 = number.substring(4, 8);
            String s3 = number.substring(8, len);
            ret = s1 + " " + s2 + " " + s3;
        } else if (len <= 16) {
            String s1 = number.substring(0, 4);
            String s2 = number.substring(4, 8);
            String s3 = number.substring(8, 12);
            String s4 = number.substring(12, len);
            ret = s1 + " " + s2 + " " + s3 + " " + s4;
        } else {
            String s1 = number.substring(0, 4);
            String s2 = number.substring(4, 8);
            String s3 = number.substring(8, 12);
            String s4 = number.substring(12, 16);
            len = len < 19 ? len : 19;
            String s5 = number.substring(16, len);
            ret = s1 + " " + s2 + " " + s3 + " " + s4 + " " + s5;
        }
        return ret;
    }

    /**
     * 根据图片的url路径获得Bitmap对象
     *
     * @param url
     * @return
     */
    public static Bitmap returnBitmap(String url) {
        String pic = "";
        try {
            pic = URLDecoder.decode(url, "UTF-8").replaceAll("\\r", "").replaceAll("\\n", "");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        byte[] bytes = Base64.decode(pic, Base64.DEFAULT);
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    }

    /**
     * 获取当月第一天
     */
    public static String getMonthFirstDay() {
        SimpleDateFormat dateFormater = new SimpleDateFormat(
                "yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.getTime();
        return dateFormater.format(cal.getTime());
    }

    /**
     * 提示弹框view
     */
    public static View generateHintView(Context mContext, int imageRes, String content) {
        LinearLayout layout = new LinearLayout(mContext);
        layout.setOrientation(LinearLayout.VERTICAL);
        if (imageRes != 0) {
            ImageView imageView = new ImageView(mContext);
            imageView.setImageResource(imageRes);
            layout.addView(imageView);
        }

        if (!TextUtils.isEmpty(content)) {
            TextView textView = new TextView(mContext);
            textView.setText(content);
            if (imageRes != 0) {
                textView.setPadding(0, ScreenUtil.dp2px(12), 0, 0);
            }
            layout.addView(textView);
        }
        return layout;
    }

    /**
     * 复制
     */
    public static void copyText(Context context, String text) {
        if (TextUtils.isEmpty(text)) return;
        if (android.os.Build.VERSION.SDK_INT > 11) {
            android.content.ClipboardManager c = (android.content.ClipboardManager) context
                    .getSystemService(CLIPBOARD_SERVICE);
            c.setPrimaryClip(ClipData.newPlainText("txt", text));
        } else {
            android.text.ClipboardManager c = (android.text.ClipboardManager) context
                    .getSystemService(CLIPBOARD_SERVICE);
            c.setText(text);
        }
        ToastUtil.showTextViewPrompt(context, "复制成功");
    }


    /**
     * 复制
     */
    public static void copyText(Context context, String text, String toastTxt) {
        if (TextUtils.isEmpty(text)) return;
        if (android.os.Build.VERSION.SDK_INT > 11) {
            android.content.ClipboardManager c = (android.content.ClipboardManager) context
                    .getSystemService(CLIPBOARD_SERVICE);
            c.setPrimaryClip(ClipData.newPlainText("txt", text));
        } else {
            android.text.ClipboardManager c = (android.text.ClipboardManager) context
                    .getSystemService(CLIPBOARD_SERVICE);
            c.setText(text);
        }
        ToastUtil.showTextViewPrompt(context, toastTxt);
    }

    public static boolean isPermissionEnabled(Context context, String permName) {
        PackageManager pm = context.getPackageManager();

        return (PackageManager.PERMISSION_GRANTED ==
                pm.checkPermission(permName, context.getPackageName()));
    }

    /**
     * 打开APP系统设置
     */
    public static void openAppSetting(Context context) {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.setting.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
        }
        IntentTool.startActivity(context, localIntent);
    }
}
